﻿Imports System.Text
Imports Microsoft.VisualStudio.TestTools.UnitTesting

Imports CQRSAzure.EventSourcing
Imports CQRSAzure.EventSourcing.Azure.Blob

<TestClass()>
Public Class AzureBlobEventStreamUnitTest

    ''' <remarks>
    ''' Note that you will either need to reference an actual Azure account or to start the azure local storage emulator in order
    ''' to run this unit test
    ''' </remarks>
    <TestMethod()>
    Public Sub Reader_Constructor_TestMethod()

        Dim testAgg As New MockAggregate("1234")
        Dim testObj As BlobEventStreamReader(Of MockAggregate, String) = BlobEventStreamReader(Of MockAggregate, String).Create(testAgg)
        Assert.IsNotNull(testObj)

    End Sub

    <TestMethod()>
    Public Sub Writer_Constructor_TestMethod()

        Dim testAgg As New MockAggregate("1234")
        Dim testObj As BlobEventStreamWriter(Of MockAggregate, String) = BlobEventStreamWriter(Of MockAggregate, String).Create(testAgg)
        Assert.IsNotNull(testObj)

    End Sub

    <TestMethod()>
    Public Sub Writer_WriteEvent_TestMethod()

        Dim testAgg As New MockAggregate("1234")
        Dim testObj As BlobEventStreamWriter(Of MockAggregate, String) = BlobEventStreamWriter(Of MockAggregate, String).Create(testAgg)
        testObj.AppendEvent(New MockEventTypeOne() With {.EventOneStringProperty = "My test", .EventOneIntegerProperty = 123})

        Assert.IsTrue(testObj.RecordCount > 0)

    End Sub

    <TestMethod()>
    Public Sub Writer_WriteEventTwo_TestMethod()

        Dim testAgg As New MockAggregate("1234")
        Dim testObj As BlobEventStreamWriter(Of MockAggregate, String) = BlobEventStreamWriter(Of MockAggregate, String).Create(testAgg)
        testObj.AppendEvent(New MockEventTypeTwo() With {.EventTwoNullableDateProperty = DateTime.UtcNow, .EventTwoStringProperty = "My test two", .EventTwoDecimalProperty = 123.45})

        Assert.IsTrue(testObj.RecordCount > 0)

    End Sub

    <TestMethod()>
    Public Sub Reader_ReadEvents_TestMethod()

        Dim testAgg As New MockAggregate("1234")
        Dim testObj As BlobEventStreamReader(Of MockAggregate, String) = BlobEventStreamReader(Of MockAggregate, String).Create(testAgg)
        Dim ret = testObj.GetEvents()

        Assert.IsNotNull(ret)

    End Sub

    <TestMethod>
    Public Sub MakeValidStorageFolderName_Empty_TestMethod()

        Dim expected As String = "uncategorised"
        Dim actual As String = ""

        actual = BlobEventStreamBase.MakeValidStorageFolderName("")

        Assert.AreEqual(expected, actual)

    End Sub

    <TestMethod>
    Public Sub MakeValidStorageFolderName_TooShort_TestMethod()

        Dim expected As String = "a-abc"
        Dim actual As String = ""

        actual = BlobEventStreamBase.MakeValidStorageFolderName("-a")

        Assert.AreEqual(expected, actual)

    End Sub

    <TestMethod>
    Public Sub MakeValidStorageFolderName_TooShortAfterTrim_TestMethod()

        Dim expected As String = "a-abc"
        Dim actual As String = ""

        actual = BlobEventStreamBase.MakeValidStorageFolderName("------a")

        Assert.AreEqual(expected, actual)

    End Sub

    <TestMethod>
    Public Sub MakeValidStorageFolderName_FixChars_TestMethod()

        Dim expected As String = "duncan-s-model"
        Dim actual As String = ""

        actual = BlobEventStreamBase.MakeValidStorageFolderName("Duncan's Model")

        Assert.AreEqual(expected, actual)

    End Sub

    <TestMethod>
    Public Sub MakeValidStorageFolderName_IsValid_TestMethod()

        Dim expected As String = "duncan-s-model"
        Dim actual As String = ""

        actual = BlobEventStreamBase.MakeValidStorageFolderName(expected)

        Assert.AreEqual(expected, actual)

    End Sub

    <TestMethod>
    Public Sub MakeValidStorageFolderName_TooLong_TestMethod()

        Dim expected As Integer = 63
        Dim actual As Integer = 0

        actual = BlobEventStreamBase.MakeValidStorageFolderName("The quick dog jumped over the lazy dog which was suprising to them both with the display of dexterity").Length

        Assert.AreEqual(expected, actual)

    End Sub

End Class